<?php

namespace app\admin\controller;

use app\admin\model\Servicearticle;
use clt\Tree;
use think\facade\Request;
use think\facade\Env;
use app\admin\model\Service as ServiceModel;
class Service extends Common
{
    protected $dao;

    function initialize() {
        parent::initialize();
        $this->dao = db('service');
    }

    public function index()
    {
        $list = $this->dao->order('listorder', 'asc')->paginate(20);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign('title', '添加分类');
        return $this->fetch();
    }

    public function insert()
    {
        $reques = input();
        $model = new ServiceModel();
        if ($model->plus($reques)) {
            cache('serviceList', null);
            $result['msg'] = '分类添加成功!';
            $result['url'] = url('index');
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '分类添加失败!';
            $result['code'] = 0;
            return $result;
        }
    }


    public function edit()
    {
        $id = input('id');
        $re = ServiceModel::get(['id'=>$id]);
        if ($re) {
            $this->assign('data',$re);
            return $this->fetch('edit');
        }
    }

    public function run_update()
    {
        $data = input();
        $model = new ServiceModel();
        if ($model->edit($data)) {
            cache('serviceList', null);
            $result['msg'] = '分类修改成功!';
            $result['url'] = url('index');
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '分类修改失败!';
            $result['code'] = 0;
            return $result;
        }
    }

    //删除
    public function delete()
    {
        $id = input('id', 0, 'intval');
        $info = ServiceModel::get($id);
        if (empty($info)) {
            $this->error('该信息不存在');
        }
        if ($info->rm()) {
            cache('serviceList', null);
            $result['info'] = '分类删除成功!';
            $result['url'] = url('index');
            $result['status'] = 1;
            return $result;
        }
    }

    public function article_list()
    {
        if(Request::isAjax()){
            $requst = input();
            $where = 'service_id = '.input('id');
            if (isset($requst['key'])) {
                $title = $requst['key'];
                unset($requst['key']);
                $where .= " and `title` like '$title%'";
            }
            $model = new Servicearticle();
            $re = $model->get_data($where, $requst);

            $res = $model::all();
            $num = count($res);

            echo json_encode(array('code' => 0, 'msg' => '', 'count' => $num, 'data' => $re));
            exit;
        }
        $this->assign('id',input('id'));
        return $this->fetch();
    }

    public function article_add()
    {
        if (Request::isAjax()){
            $requst = input();
            $model = new Servicearticle();
            if ($model->plus($requst)) {
                $result['msg'] = '数据添加成功!';
                $result['url'] = url('article_list',array('id'=>$requst['service_id']));
                $result['code'] = 1;
                return $result;
            } else {
                $result['msg'] = '数据添加失败!';
                $result['code'] = 0;
                return $result;
            }
        }
        $this->assign ( 'service_id', input('id') );
        $this->assign ( 'title', '添加内容' );
        return $this->fetch();
    }

    public function article_edit()
    {
        if (Request::isAjax()) {
            $data = input();
            $model = new Servicearticle();
            if ($model->edit($data)) {
                $result['msg'] = '数据修改成功!';
                $result['url'] = url('article_list',array('id'=>$data['service_id']));
                $result['code'] = 1;
                return $result;
            } else {
                $result['msg'] = '数据修改失败!';
                $result['code'] = 0;
                return $result;
            }
        }
        $id = input('id', 0, 'intval');
        $info = Servicearticle::get($id);

        if (empty($info)) {
            $this->error('该信息不存在');
        }
        $this->assign('data', $info);
        return $this->fetch();
    }

    public function article_del()
    {
        $id = input('id', 0, 'intval');
        $info = Servicearticle::get($id);
        if (empty($info)) {
            $this->error('该信息不存在');
        }
        if ($info->rm()) {
            $result['msg'] = '数据删除成功!';
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '删除失败!';
            $result['code'] = 0;
            return $result;
        }
    }

    #排序
    public function sort()
    {
        $re = input();
        $model = new Servicearticle();
        if ($model->sort($re)) {
            $result['msg'] = '更新成功!';
            $result['code'] = 1;
            return $result;
        } else {
            $result['msg'] = '更新失败!';
            $result['code'] = 0;
            return $result;
        }
    }

    public function delAll(){
        $map =input('param.ids/a');
        $model = new Servicearticle();
        $model::destroy($map);
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        return $result;
    }

}